package com.bhald.CJ.MethodView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.androidquery.AQuery;
import com.bhald.CJ.Common.CommonM;
import com.bhald.CJ.Common.CommonaddView;
import com.bhald.CJ.lock.LoginActivity;
import com.bhald.CJ.lock.R;

public class SetLayout_Update implements OnClickListener {

	/*
	 * update pass
	 */

	private LinearLayout updatelayout;
	private AQuery query;
	private Context context;
	private View update_layout;
	private String Pass;
	private SharedPreferences user;

	//
	public SetLayout_Update(LinearLayout updatelayout, Context context) {
		this.updatelayout = updatelayout;
		this.context = context;
		query = new AQuery(context);
	}

	public void Start() {
		update_layout = CommonaddView.addView(updatelayout, context,
				R.layout.set_layout_main_update_user);
		query.id(update_layout.findViewById(R.id.set_layout_update_btn))
				.clicked(this);

		//
		query.id(update_layout.findViewById(R.id.set_layout_update_oldpass))
				.getEditText().addTextChangedListener(mTextWatcher);
		query.id(update_layout.findViewById(R.id.set_layout_update_newoksspass))
				.getEditText().addTextChangedListener(mTextWatchers);
		//
		user = context.getSharedPreferences("userinfoss", 0);
		Pass = user.getString("upswd", null);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.set_layout_update_btn:

			String oldpass = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_oldpass))
					.getText().toString();
			String newuser = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_newpass))
					.getText().toString();
			String newokpass = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_newokpass))
					.getText().toString();
			String newokspass = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_newoksspass))
					.getText().toString();
			if (newokspass.endsWith(newokpass) && oldpass.equals(Pass)) {
				SharedPreferences sps = context.getSharedPreferences(
						"userinfoss", Context.MODE_PRIVATE);
				Editor editor = sps.edit();
				editor.putString("uname", newuser);
				editor.putString("upswd", newokpass);
				editor.putBoolean("auto", false);
				editor.commit();

				CommonM.ShowSmallDialog(context, context.getResources()
						.getString(R.string.update_ok), R.drawable.cry_s);

				context.startActivity(new Intent(context, LoginActivity.class));
			}

			break;

		default:
			break;
		}

	}

	TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			String oldp = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_oldpass))
					.getText().toString();
			if (!oldp.equals(Pass)) {
				query.id(update_layout.findViewById(R.id.update_tip))
						.textColor(Color.RED)
						.text(context.getResources().getString(
								R.string.passerror));
			} else {
				query.id(update_layout.findViewById(R.id.update_tip))
						.textColor(Color.GREEN)
						.text(context.getResources().getString(R.string.passok));
			}
		}
	};

	TextWatcher mTextWatchers = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int arg1, int arg2,
				int arg3) {
		}

		@Override
		public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			String newp = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_newokpass))
					.getText().toString();
			String newokp = query
					.id(update_layout
							.findViewById(R.id.set_layout_update_newoksspass))
					.getText().toString();
			if (!newokp.equals(newp)) {
				query.id(update_layout.findViewById(R.id.update_tips))
						.textColor(Color.RED)
						.text(context.getResources().getString(
								R.string.passerror));
			} else {
				query.id(update_layout.findViewById(R.id.update_tips))
						.textColor(Color.GREEN)
						.text(context.getResources().getString(R.string.passok));
			}
		}
	};
}
